<template>
  <div 
    class="gpu-node"
    :class="statusClass"
    @mouseenter="showTooltip = true"
    @mouseleave="showTooltip = false"
  >
    <div class="gpu-icon">
      <div class="gpu-chip" :style="gpuTempStyle"></div>
      <div class="gpu-fan" :style="fanSpeedStyle"></div>
    </div>
    <div class="gpu-info">
      <div class="gpu-id">GPU-{{ gpuId }}</div>
      <div class="gpu-utilization">
        <div class="util-bar" :style="utilizationStyle"></div>
        <span>{{ utilization }}%</span>
      </div>
    </div>
    
    <div v-if="showTooltip" class="gpu-tooltip">
      <div class="tooltip-row"><strong>Status:</strong> {{ status }}</div>
      <div class="tooltip-row"><strong>GPU:</strong> {{ details.gpu }}</div>
      <div class="tooltip-row"><strong>Temp:</strong> {{ temperature }}°C</div>
      <div class="tooltip-row"><strong>Utilization:</strong> {{ utilization }}%</div>
      <div class="tooltip-row"><strong>Batch Size:</strong> {{ details.batch }}</div>
      <div class="tooltip-row"><strong>LR:</strong> {{ details.lr }}</div>
      <div class="tooltip-row"><strong>Optimizer:</strong> {{ details.opt }}</div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'

const props = defineProps({
  status: String,
  details: Object,
  index: Number,
  groupId: Number
})

const showTooltip = ref(false)

// Extract GPU information from details
const gpuInfo = computed(() => {
  const gpuMatch = props.details.gpu.match(/GPU-(\d+).*Util: (\d+)%.*Temp: (\d+)C/)
  return {
    id: gpuMatch ? gpuMatch[1] : props.index,
    utilization: gpuMatch ? parseInt(gpuMatch[2]) : 0,
    temperature: gpuMatch ? parseInt(gpuMatch[3]) : 40
  }
})

const gpuId = computed(() => props.groupId * 3 + props.index)
const utilization = computed(() => gpuInfo.value.utilization)
const temperature = computed(() => gpuInfo.value.temperature)

const statusClass = computed(() => {
  if (props.status.includes('Not Training')) {
    return 'status-offline'
  } else if (props.status.includes('Training')) {
    return 'status-active'
  }
  return 'status-idle'
})

// Dynamic styles
const utilizationStyle = computed(() => ({
  width: `${utilization.value}%`,
  backgroundColor: getUtilizationColor(utilization.value)
}))

const gpuTempStyle = computed(() => ({
  backgroundColor: getTemperatureColor(temperature.value)
}))

const fanSpeedStyle = computed(() => ({
  animationDuration: `${1 + (utilization.value / 100)}s`
}))

// Helper functions
function getUtilizationColor(util) {
  if (util > 80) return '#ff4d4f'
  if (util > 50) return '#faad14'
  return '#52c41a'
}

function getTemperatureColor(temp) {
  if (temp > 70) return '#ff4d4f'
  if (temp > 60) return '#fa8c16'
  return '#52c41a'
}
</script>

<style scoped>
.gpu-node {
  position: relative;
  width: 120px;
  padding: 15px;
  border-radius: 8px;
  background-color: #f5f5f5;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  transition: all 0.2s;
  cursor: pointer;
}

.gpu-node:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.status-active {
  border-left: 4px solid #52c41a;
}

.status-idle {
  border-left: 4px solid #faad14;
}

.status-offline {
  border-left: 4px solid #ff4d4f;
  opacity: 0.7;
}

.gpu-icon {
  position: relative;
  width: 60px;
  height: 40px;
  margin: 0 auto 10px;
  background-color: #333;
  border-radius: 4px;
  padding: 5px;
}

.gpu-chip {
  width: 40px;
  height: 30px;
  margin: 5px auto;
  border-radius: 3px;
  transition: background-color 0.3s;
}

.gpu-fan {
  position: absolute;
  right: 5px;
  top: 5px;
  width: 10px;
  height: 10px;
  background-color: #aaa;
  border-radius: 50%;
  animation: spin linear infinite;
}

.gpu-fan::before,
.gpu-fan::after {
  content: '';
  position: absolute;
  top: 0;
  left: 4px;
  width: 2px;
  height: 10px;
  background-color: #666;
  transform-origin: center;
}

.gpu-fan::before {
  transform: rotate(0deg);
}

.gpu-fan::after {
  transform: rotate(90deg);
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

/* 根据GPU利用率调整风扇速度 */
.status-active .gpu-fan {
  animation-duration: calc(0.5s + (100 - v-bind('utilization')) * 0.03s);
}

.status-idle .gpu-fan {
  animation-duration: 2s;
  opacity: 0.7;
}

.status-offline .gpu-fan {
  animation: none;
  opacity: 0.3;
}

.gpu-info {
  text-align: center;
}

.gpu-id {
  font-weight: bold;
  margin-bottom: 5px;
}

.gpu-utilization {
  display: flex;
  align-items: center;
  gap: 5px;
}

.util-bar {
  height: 10px;
  border-radius: 5px;
  transition: all 0.3s;
}

.gpu-tooltip {
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  width: 220px;
  background-color: #222;
  color: white;
  padding: 10px;
  border-radius: 6px;
  z-index: 10;
  margin-bottom: 10px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.2);
  font-size: 0.9em;
}

.tooltip-row {
  margin-bottom: 5px;
}

.tooltip-row:last-child {
  margin-bottom: 0;
}
</style>